import de.fusionfactory.tools.Files
import org.codehaus.groovy.ant.FileScanner

Boolean targetWritable(String relPath) {
    Files.resolveRelative(relPath).canWrite()
}



def ant = new AntBuilder()

if(args.size() < 2) {
    throw new IllegalArgumentException("Usage: Extract [dir of archives] [target dir]")
}

String archivePath = args[0];
String extractPath = args[1];
File archiveDir = Files.resolveRelative( archivePath)
File extractDir = Files.resolveRelative( extractPath)

if( !(archiveDir.exists() && archiveDir.isDirectory()) ) {
    def msg = "Unable to find archive data directory with relative path: \n" <<
              "'$archivePath'"
    throw new FileNotFoundException(msg)
}

if( !targetWritable(extractPath)) {
    def msg = "Unable to write in given target directory directory with relative path:" <<
              "'$extractPath'"
    throw new FileNotFoundException(msg)
}


FileScanner files = ant.fileScanner() {
    fileset(dir: args[0]) {
        include(name: '*.tar.gz')
    }
}

files.each { file ->
    println file.inspect()
    ant.untar(src: file, dest: extractDir, compression: 'gzip')
}
